package org.wenhuayuan.project.main.demo

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import kotlinx.android.synthetic.main.activity_demo_list.*
import org.wenhuayuan.library.util.HiDataBus
import org.wenhuayuan.project.main.R

class DemoListActivity : AppCompatActivity() {
    private val liveData = NewLiveData<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo_list)
        lifecycle.addObserver(LocationObserver())

//        liveDataMethod1()
        liveDataMethod2()

        HiDataBus.with<String>("hello-message").observerSticky(this, false, object : Observer<String>{
            override fun onChanged(t: String?) {
                Log.e("HiDataBus", t?:"不能传递空数据")
            }
        })
        btn_hidatabus.setOnClickListener {
            HiDataBus.with<String>("hello-message").setStickyData("hello world , here send sticky message.")
        }
    }

    private fun liveDataMethod2() {
        liveData.observe(this, object:Observer<String>{
            override fun onChanged(t: String?) {
                Log.e("DemoListActivity", "onChanged-t1="+t)
            }
        })
        liveData.observe(this, object:Observer<String>{
            override fun onChanged(t: String?) {
                Log.e("DemoListActivity", "onChanged-t2="+t)
            }
        })

        liveData.postNewData("liveData.observe@DemoListActivity")
    }

    override fun onResume() {
        super.onResume()
        lifecycle.addObserver(LocationLifecycleEventObserver())


        liveData.observe(this, object:Observer<String>{
            override fun onChanged(t: String?) {
                Log.e("DemoListActivity", "onChanged-onResume="+t)
            }
        })
    }

    private fun liveDataMethod1() {
        //创建两个长得差不多的LiveData对象
        val liveData1 = MutableLiveData<Integer>()
        val liveData2 = MutableLiveData <Integer>()

        val observer = Observer<Integer> { t ->
            //一旦liveData或liveData发送了新的数据 ，observer便能观察的到，以便统一处理更新UI
            Log.e("DemoListActivity", "onChanged-t="+t)
        }

        //再创建一个聚合类MediatorLiveData
        val liveDataMerger = MediatorLiveData<Integer>()
        //分别把上面创建LiveData 添加进来。
        liveDataMerger.addSource(liveData1, observer)
        liveDataMerger.addSource(liveData2, observer)

    }


    class NewLiveData<T> : LiveData<T>() {
        fun postNewData (message:T) {
            postValue(message)
        }
    }

    class LocationObserver : LifecycleObserver {
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        fun start(owner:LifecycleOwner) {
            Log.e("DemoListActivity.kt", "start")
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        fun resume(owner:LifecycleOwner) {
            Log.e("DemoListActivity.kt", "resume")
        }
    }

    class LocationLifecycleEventObserver : LifecycleEventObserver {

        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            Log.e("DemoListActivity.kt", "LocationLifecycleEventObserver-onStateChanged")
        }

    }

    class LocalViewModel(private val viewmodelHandle: SavedStateHandle) : ViewModel() {
        fun saveBundleData(data:String) {
            viewmodelHandle.set("", data)
        }

    }

}
